package com.ycd._02_xhr;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class _02_consumer {

    public static void main(String[] args) throws Exception { // 1.获取producer对象
        // 1.获取consumer 对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("xhr_consumer_group");
        // 2.设置nameserver
        consumer.setNamesrvAddr("localhost:9876");
        // 3.订阅一个或者多个Topic-主题，以及Tag来过滤需要消费的消息
        consumer.subscribe("xhr","dddd");
        // 4.注册回调实现类来处理从broker拉取回来的消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext context) {
                for (MessageExt message : list) {
                    System.out.println(new String(message.getBody(), StandardCharsets.UTF_8));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        // 5.启动
        consumer.start();
    }
}
